package com.interest.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONPOJOBuilder;
import com.interest.bean.message.Article;
import com.interest.bean.message.BaseRespMessage;
import com.interest.bean.message.resp.ImageMessage;
import com.interest.bean.message.resp.NewsMessage;
import com.interest.bean.message.resp.TextMessage;
import com.interest.util.HttpConUtil;
import com.interest.util.JsonUtil;
import com.interest.util.MessageUtil;

import ch.qos.logback.core.pattern.SpacePadder;

public class MessageServiceImp implements MessageService {
	public String delMessage(HttpServletRequest request) {
		String respMessage = "";
		try {
			Map<String, String> requestMap = MessageUtil.xmlToJson(request);
			System.out.println(requestMap);
			// 当前账号id
			String fromUserName = requestMap.get("FromUserName");
			// openId
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");
			TextMessage textMessage = new TextMessage();
			textMessage.setContent("欢迎来到笑傲天的测试账号");
			BaseRespMessage resp = textMessage;

			switch (msgType) {
			case "text":
				resp = delTextMessage(requestMap);
				break;
			case "image":
				resp = delImageMessage(requestMap);
				break;
			default:
				break;
			}
			resp.setFromUserName(toUserName);
			resp.setToUserName(fromUserName);
			resp.setFuncFlag(0);
			if (resp instanceof TextMessage) {
				respMessage = MessageUtil.textMessageToXml((TextMessage) resp);
			} else if (resp instanceof ImageMessage) {

			}else if(resp instanceof NewsMessage){
				respMessage = MessageUtil.newsMessageToXml((NewsMessage) resp);
			}

			System.err.println(respMessage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return respMessage;
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		map.put("Content", "你好");
		new MessageServiceImp().delTextMessage(map);
	}

	/**
	 * 使用 图灵机器人 进行交互
	 * 
	 * @param requestMap
	 * @return
	 */
	public BaseRespMessage delTextMessage(Map<String, String> requestMap) {
		boolean special = false;
		BaseRespMessage respMessage = null;
		// String urlStr =
		// "http://dev.skjqr.com/api/weixin.php?email=yangminghao_91@163.com&appkey=034bbf6b009d64221c33d20beda9f971&msg=";
		String urlStr = "http://www.tuling123.com/openapi/api";
		String msg = requestMap.get("Content");
		try {
			msg = HttpConUtil.doPost(urlStr, "key=eb9078f2ef6e4b89a78d18e276872136&info="+msg);
			JSONObject respJson = JSONObject.parseObject(msg);
			System.out.println(respJson);
			int code = respJson.getIntValue("code");
			String content = respJson.getString("text");
			TextMessage textMessage = new TextMessage();
			if (200000 == code) {
				// ImageMessage imageMessage = new ImageMessage();
				String imageUrl = respJson.getString("url");
				content = "<a href=\"" + imageUrl + "\">" + content + "</a>";
			}
			textMessage.setContent(content);
			respMessage = textMessage;
			if (302000 == code) {
				JSONArray list = (JSONArray)JsonUtil.getSubListFromJSON(respJson, "list");
				NewsMessage newMessage = new NewsMessage();
				List<Article> articles = new ArrayList<Article>();
				list.forEach(item->{
					if(item instanceof JSONObject){
						Article  article = new Article();
						JSONObject articleJson = (JSONObject)item;
						String articleStr = articleJson.getString("article");
						String icon = articleJson.getString("icon");
						String detailUrl = articleJson.getString("detailurl");
						article.setTitle(articleStr);
						article.setUrl(detailUrl);
						article.setPicUrl(icon);
						article.setDescription(articleStr);
						articles.add(article);
					}
				});
				
				newMessage.setArticles(articles);
				newMessage.setArticleCount(list.size());
				respMessage = newMessage;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respMessage;
	}

	public BaseRespMessage delImageMessage(Map<String, String> requestMap) {
		return null;
	}

	public BaseRespMessage delNewsMessage(Map<String, String> requestMap) {
		return null;
	}
}
